const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: wx.getStorageSync('userInfo'),
    loading: false
  },

  // 跳转
  checkboxAll: function() {
    wx.navigateTo({
      url: './opinionindex/opinionindex'
    })
  },

  // 表单提交事件
  formSubmit: function (e) {
    let that = this;
    let params = e.detail.value;
    let opinion = e.detail.value.opinion;
    let email = e.detail.value.email;
    let regEmail = /^[a-z\d_\-\.]+@[a-z\d_\-]+\.[a-z\d_\-]+$/i;
    if (opinion == '' || this.isNull(opinion)) {
      wx.showModal({
        title: '提示',
        content: '反馈内容不能为空!',
        showCancel: false
      })
      return false;
    }
    if (email != '' && !regEmail.test(email)) {
      wx.showModal({
        title: '提示',
        content: '您输入的电子邮箱不符合规则!',
        showCancel: false
      })
      return false;
    }
    this.setData({
      loading: true
    })
    params.openId = wx.getStorageSync('userInfo').openid;
    params.CardName = wx.getStorageSync('userInfo').userName || '';
    params.CardCode = wx.getStorageSync('userInfo').CardCode || '';
    console.log(params)
    wx.request({
      url: globalIp + 'home/insertOpinion',
      data: {
        opinion: params.opinion,
        email: params.email,
        openId: params.openId,
        cardName: params.CardName,
        cardCode: params.CardCode,
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          that.setData({
            loading: false
          })
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1000,
            success: function(res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1  // 返回页数：1
                })
              }, 1000)
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000,
            success: function(res) {
              setTimeout(() => {
                that.setData({
                  loading: false
                })
              }, 2000)
            }
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '发生错误，请重新进入小程序',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 判断字符串是否都为空格
  isNull: function(str) {
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str); 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})